package com.sdu.game.controller;


import com.sdu.game.mapper.ArticleMapper;
import com.sdu.game.mapper.CommentMapper;
import com.sdu.game.mapper.UserMapper;
import com.sdu.game.pojo.Article;
import com.sdu.game.pojo.ArticleExample;
import com.sdu.game.pojo.User;
import com.sdu.game.pojo.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/zhilang/v1/user/register")
    //http://localhost:8080/zhilang/v1/user/register?password=123456&userId=0004&username=胡皓翔
    public int register(String username,String password,int userId){

        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.or();
        criteria.andUserIdEqualTo(userId);

        List<User> list=userMapper.selectByExample(userExample);

        int flag=1;
        if(list.size()>=1){
            System.out.println(list.toString());
            flag=0;
            return flag;

        }else{
            User user=new User();
            user.setUsername(username);
            user.setUserId(userId);
            user.setPassword(password);

            int insertRow=userMapper.insertSelective(user);
            if (insertRow>=1){
                System.out.println("注册成功");
                return flag;
            }else {
                // 存在
                flag=0;
                System.out.println("注册失败");
                return flag;
            }


        }


    }


    //用户登录
    @RequestMapping("/zhilang/v1/user/login")
    //http://localhost:8080/zhilang/v1/user/login?userId=1&password=111
    public User login(int userId,String password){
        //对user_id进行判断，返回user类
        //select * from user where userId='a' and password='a'
        UserExample userExample=new UserExample();
        //UserExample.Criteria是UserExample中的内部类
        UserExample.Criteria criteria=userExample.or();
        criteria.andUserIdNotEqualTo(userId);
        criteria.andPasswordEqualTo(password);

        List<User> list=userMapper.selectByExample(userExample);
        if (list.size()>=1){
            //登录成功
            User user=list.get(0);
            user.setPassword("");//密码不能返回到浏览器
            return user;
        }else {
            //登录失败
            return null;//{state:1,msg:登录失败}
        }

        //return new User();
    }


    //用户信息显示
    @RequestMapping("/zhilang/v1/user/userOut")
    //http://localhost:8080/zhilang/v1/user/login?userId=1
    public User userOut(int userId){
        //数据库查询
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.or();
        criteria.andUserIdNotEqualTo(userId);

        List<User> list=userMapper.selectByExample(userExample);
        if (list.size()>=1){
            //找到用户信息
            User user=list.get(0);
            user.setPassword("");//密码不能返回到浏览器
            return user;
        }else {
            //没找到用户，返回空
            return null;//{state:1,msg:登录失败}
        }

        // return new User();

    }


    //用户信息更改
    @RequestMapping("/zhilang/v1/user/userUpdate")
    //http://localhost:8080/zhilang/v1/user/login?userId=1&password=111&username=小蓝&signatur=go&email=123@qq.com&mobile=135088882222
    public String userUpdate(User user) {
        //数据库查询
        int updateRow=userMapper.updateByPrimaryKeySelective(user);
        if (updateRow>=1){
            return  "修改成功";
        }else {
            return "修改失败";
        }

        //return 1;
    }


    //删除用户
    @RequestMapping("/zhilang/v1/user/delete")
    // http://localhost:8080/zhilang/v1/user/login?userId=1
    public String delete(Integer userId){
        int deleteRow=userMapper.deleteByPrimaryKey(userId);
        if (deleteRow>=1){
            return "删除成功";
        }else {
            return "删除失败";
        }
    }






}
